<?php

namespace frontend\models;

/**
 * @author  shuang
 * @date    2016-9-6 13:02:13
 * @version V1.0
 * @desc    
 */
use common\models\UserProfile;
use Yii;
use yii\db\Query;
use frontend\models\WB_UserAccount;
use frontend\models\WB_UserPayRecord;
use frontend\models\WB_Grade;

class WB_UserProfile extends UserProfile {
    /*
     * 查询用户基本信息
     * @params $userid
     * return array
     */

    public static function getInfo($userid) {
        $query = new Query();
        $query->from(WB_UserProfile::tableName() . " as a");
        $query->leftJoin(WB_UserAccount::tableName() . " as b", "a.userid=b.userid");
        $query->leftJoin(WB_Grade::tableName() . " as c", "a.gradeid=c.id");
        $query->select("a.username,b.amount,b.rebate_amount,a.alipay,a.gradeid,c.name as gradename,a.phone,a.qq,a.truename");
        $query->where("a.userid=:userid", array(":userid" => $userid));
        $query->addSelect("(SELECT SUM(amount) FROM " . WB_UserPayRecord::tableName() . " WHERE userid=$userid and type=1 and (class_type=1 or class_type=8)) AS payamount");
        return $query->one();
    }

    /*
     * 查询代理详情
     * @params $invite_code
     * return array
     */

    public static function getAgentDetail($invite_code) {
        $query = new Query();
        $query->from(WB_UserProfile::tableName() . " as a");
        $query->leftJoin(WB_User::tableName() . " as b", "a.userid=b.id");
        $query->leftJoin(WB_UserAccount::tableName() . " as c", "a.userid=c.userid");
        $query->leftJoin(WB_Grade::tableName() . " as d", "a.gradeid=d.id");
        $query->leftJoin("(SELECT COUNT(id) AS taskcount, userid FROM me_task GROUP BY userid) AS e", "a.userid=e.userid");
        $query->select("b.last_login_at,b.created_at,a.qq,a.username,d.`name` AS gradename,c.`amount`,e.taskcount");
        $query->where("a.invite_code=:invite_code", array(":invite_code" => $invite_code));
        $countQuery = clone $query;
        $pagesize = 15;
        $offset = (Yii::$app->request->get("page") - 1) * $pagesize;
        $limit = $pagesize;
        $pager = new \yii\data\Pagination(['totalCount' => $countQuery->count(), 'defaultPageSize' => $pagesize]);
        $res = $query->offset($offset)->limit($limit)->all();
        return ["pager" => $pager, "data" => $res];
    }

}
